﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_StunningShout : TargetSpell
    {
        public Spell_StunningShout() { }

        public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            //Resume a normal stun with this spell icon.

            Spell_Stun stunSpell = new Spell_Stun();
            stunSpell.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            List<CreatureBase> creaturesToUpdate = new List<CreatureBase>();
            foreach (CreatureBase creaturebase in myCreatureBase.currentMapItem.CreaturesBases)
            {
                if (creaturebase.HP > 0 &&
                    (creaturebase.Faction != TargetCreatureBase.Faction || myCreatureBase.hateManager.GetCreatureHateInfo(creaturebase) != null) &&
                    creaturebase.buffManager.GetBuffByStackType(spellIcon.StackType) == null)
                {
                    Spell_Stun stunSpell = new Spell_Stun();

                    bool isImmune = false;
                    if (creaturebase is Creature)
                    {
                        Creature targetCrit = (Creature)creaturebase;
                        if (targetCrit.isImmune(spellIcon.StackType) == true)
                        {
                            myCreatureBase.SendMessage(creaturebase.Name + " is immune to " + spellIcon.Name + "!", "spelldefense");
                            isImmune = true;
                        }
                    }

                    if (isImmune == false)
                    {
                        if (stunSpell.tryResist(spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, creaturebase))
                        {
                            creaturebase.SendMessage("You resist " + myCreatureBase.Name + "'s " + spellIcon.Name, "spelloffense");
                            myCreatureBase.SendMessage(creaturebase.Name + " resists your " + spellIcon.Name, "spelloffense");
                            continue;
                        }

                        CreatureBase cb = creaturebase;
                        stunSpell.Resume(cb, this.spellIcon, -1, CastLevel, CastingCharacterID, CastingCreatureInt, CastingCreatureWis, -1, null);
                        cb.buffManager.UpdateBuffs();

                        creaturesToUpdate.Add(cb);

                        Message msg = new Message();
                        msg.CreatureID = cb.CreatureID;
                        msg.Text = cb.Name + " is stunned!";
                        msg.Type = "spelloffense";
                        cb.MessageBroadcast(cb.currentMapItem.mapItemsWithin4, msg, false);
                    }
                }
            }

            foreach (MapItem mapitem in myCreatureBase.currentMapItem.mapItemsWithin4)
            {
                foreach (CreatureBase cb in mapitem.CreaturesBases)
                {
                    if (cb is Character)
                    {
                        Character character = (Character)cb;

                        foreach (CreatureBase cbToUpdate in creaturesToUpdate)
                            character.AddCreatureBase(cbToUpdate);
                    }
                }
            }
        }
    }
}
